const db = require('../db')

const router = require('express').Router()
// 购物车
router.get('/gw', (req, res) => {
    // console.log(req.query); 
    db.query('select * from gm_shopc where user_id =' + req.query.user_id, (error, result) => {
        if (error) {
            console.log(error);
        } else {

            let ids = []
            if (result.length == 0) {
                res.json({
                    'ok': 0
                })
                return
            }
            result.forEach(element => {
                ids.push(element.goods_id)
            });
            let id = ids.join(',')
            // console.log(id);
            db.query(`select * from gm_goods where id in (${id})`, (err, resu) => {
                //  console.log(resu);
                if (err) {
                    console.log(err);
                } else {
                    // console.log(result);
                    // console.log(resu);
                    res.json({
                        "ok": 1,
                        "resu": resu,
                        "data": result
                    })
                }
            })
        }
    })
})
// 商品数量添加
router.post('/step', (req, res) => {
    // console.log(req.body);
    const num = req.body.num;
    const id = req.body.id;
    db.query(`update gm_shopc set num = ${num} where id = ${id}`, (error, result) => {
        if (error) {
            console.log(error);

        }
    })

})
// 购物车删除
router.delete('/deletes', (req, res) => {
    let id = req.query.id
    // console.log(id);

    db.query('delete from gm_shopc where id =' + id, (error, result) => {
        if (error) {
            console.log(error);
        } else {
            res.json({
                'ok': 1
            })
        }
    })
})
// 地址列表
router.get('/site', (req, res) => {
    // console.log(req.query.id==undefined);
    // console.log(req.query.id == '' || req.query.id == undefined);
    if (req.query.id == '' || req.query.id == undefined) {
        let a = 'T'
        db.query(`select * from gm_shop_site where mor = '${a}'`, (error, result) => {
            if (error) {
                console.log(error);
            } else {
                if (result.length == 0) {
                    res.json({
                        'ok': 0
                    })
                } else {
                    res.json({
                        'ok': 1,
                        'data': result
                    })
                }

            }
        })
    } else {
        db.query('select * from gm_shop_site where id =' + req.query.id, (error, result) => {
            if (error) {
                console.log(error);
            } else {
                res.json({
                    'ok': 1,
                    'data': result
                })
            }
        })
    }
})
// 地址查看操作表
router.get('/seladdress', (req, res) => {
    db.query('select * from gm_shop_site', (error, result) => {
        if (error) {
            console.log(error);
        } else {
            res.json({
                'ok': 1,
                'data': result
            })
        }
    })
})
// 添加地址
router.post('/addres', (req, res) => {
    let userid = req.body.user_id
    let name = req.body.center.name
    let tel = req.body.center.tel
    let province = req.body.center.province
    let city = req.body.center.city
    let county = req.body.center.county
    let gpr = req.body.center.addressDetail
    let address = province + city + county + gpr
    let mor = req.body.center.isDefault ? 'T' : 'F'
    if (mor == 'F') {
        db.query('insert into gm_shop_site (user_id,name,tel,province,city,county,gpr,mor,address) values (?,?,?,?,?,?,?,?,?)', [userid, name, tel, province, city, county, gpr, mor, address], (error, result) => {
            if (error) {
                console.log(error)
            } else {
                res.json({
                    "ok": 1
                })
            }
        })
    } else {
        let t = 'T'
        let f = 'F'
       db.query(`select * from gm_shop_site where mor = '${t}'`,(err,data)=>{
        //    console.log(data);
           if(err){
               console.log(err);
           }else{
               if(data.length === 0){
                db.query('insert into gm_shop_site (user_id,name,tel,province,city,county,gpr,mor,address) values (?,?,?,?,?,?,?,?,?)', [userid, name, tel, province, city, county, gpr, mor, address], (error, result) => {
                    if (error) {
                        console.log(error)
                    } else {
                        res.json({
                            "ok": 1
                        })
                    }
                })
                return
               }
              db.query(`update gm_shop_site set mor = '${f}' where id = ${data[0].id}`,(erro,datas)=>{
                  if(erro){
                      console.log(erro);
                  }else{
                    db.query('insert into gm_shop_site (user_id,name,tel,province,city,county,gpr,mor,address) values (?,?,?,?,?,?,?,?,?)', [userid, name, tel, province, city, county, gpr, mor, address], (error, result) => {
                        if (error) {
                            console.log(error)
                        } else {
                            res.json({
                                "ok": 1
                            })
                        }
                    })
                  }
              })
           }          
        //    console.log(data[0].id);
           
       })
    }
})
// 删除地址
router.delete('/deledz', (req, res) => {
    // console.log(req.query.id);

    db.query('delete from gm_shop_site where id =' + req.query.id, (error, result) => {
        if (error) {
            console.log(eror);
        } else {
            res.json({
                "ok": 1
            })
        }
    })
})
// 订单商品
router.get('/order_goods', (req, res) => {
    let ids = req.query.ids.join(',')
    // console.log(req.query.ids.join(','));
    db.query(`select * from gm_goods  where id in (${ids})`, (error, result) => {
        if (error) {
            console.log(error);
        } else {
            res.json({
                'ok': 1,
                'data': result
            })
        }
    })
})
// 订单商品购物车 ordergw
router.get('/ordergw', (req, res) => {
    let ids = req.query.ids.join(',')
    db.query(`select * from gm_shopc  where goods_id in (${ids})`, (error, result) => {
        if (error) {
            console.log(error);
        } else {            
            res.json({
                'ok': 1,
                'data': result
            })
        }
    })
})
// 为您推荐
router.get('/tuij', (req, res) => {
    // let a = Math.floor(Math.random()*60)
    let c = []
    for (var i = 0; i <= 20; i++) {
        c.push(Math.floor(Math.random() * 38))
    }
    // console.log(c.join(','));

    db.query(`select * from gm_goods where id in (${c.join(',')})`, (error, result) => {
        if (error) {
            console.log(error);
        } else {
            res.json({
                'ok': 1,
                'data': result
            })

        }
    })
})
// 提交订单
router.post('/porder', (req, res) => {
    // console.log(req.body);
   let goods_id = req.body.goods_id.join('')
   let user_id = req.body.user_id
   let order_num = req.body.order_num
   let address_id = req.body.address_id
   let status = req.body.status
   let addtime = req.body.addtime
   let sumprice = req.body.sumprice
    db.query('insert into gm_shop_order (goods_id,user_id,order_num,address_id,status,addtime,sumprice) values (?,?,?,?,?,?,?)',
    [goods_id,user_id,order_num,address_id,status,addtime,sumprice],(error,result)=>{
        if(error){
            console.log(error);
        }else{
            res.json({
                'ok':1
            })
        }
    })
})
// 获取订单
router.post('/pays',(req,res)=>{
    // console.log(req.body);
    
    db.query('select * from gm_shop_order where user_id ='+req.body.userid,(error,result)=>{
        if(error){
            console.log(error);
        }else{
            res.json({
                'ok':1,
                'data':result.pop()
            })
            // console.log(result.pop());
            
        }
    })
})
// 支付
router.post('/zfpay',(req,res)=>{
    // console.log(req.body);
    db.query('select * from shop_users where id = ? and pay_password = ?',[req.body.userid,req.body.paypassword],(error,result)=>{
        if(error){
            console.log(error)
        }else{
            if(result.length !== 0){
                // let status = 1
                // console.log(result.pop().id);     
                db.query('update gm_shop_order set status = ? where id =?',[1,req.body.orderid],(err,result)=>{
                    if(err) {
                        console.log(err);
                    }else{
                        res.json({
                            'ok':1
                        })
                    }
                })
            }
            // console.log(result);
            
        }
    })
    
}),
//不支付  保存订单
router.post('/zfpays',(req,res)=>{
    // console.log(req.body);
    db.query('select * from shop_users where id = ?',[req.body.userid,req.body.paypassword],(error,result)=>{
        if(error){
            console.log(error)
        }else{
            if(result.length !== 0){
                // let status = 1
                // console.log(result.pop().id);     
                db.query('update gm_shop_order set status = ? where id =?',[2,req.body.orderid],(err,result)=>{
                    if(err) {
                        console.log(err);
                    }else{
                        res.json({
                            'ok':1
                        })
                    }
                })
            }
            // console.log(result);
            
        }
    })
    
}),
// 获取订单状态
router.post('/orderse',(req,res)=>{
    // console.log(req.body.userid);
    db.query('select goods_id from gm_shop_order where user_id = ? and status = ?',[req.body.userid,1],(error,result)=>{
        if(error){
            console.log(error);
        }else{
            // console.log(result);   
            let idids = []
            result.forEach(element => {
                idids.push(element.goods_id)
            });
            db.query(`select * from gm_goods where id in (${idids.join(',')})`,(err,rest)=>{
                if(err){
                    console.log(err);
                }else{
                    res.json({
                        'ok':1,
                        'data':rest
                    })
                }
            }) 
        }
    })
    
})
// 获取订单状态
router.post('/orderses',(req,res)=>{
    // console.log(req.body.userid);
    db.query('select goods_id from gm_shop_order where user_id = ? and status = ?',[req.body.userid,2],(error,result)=>{
        if(error){
            console.log(error);
        }else{
            // console.log(result);   
            let idids = []
            result.forEach(element => {
                idids.push(element.goods_id)
            });
            db.query(`select * from gm_goods where id in (${idids.join(',')})`,(err,rest)=>{
                if(err){
                    console.log(err);
                }else{
                    res.json({
                        'ok':1,
                        'data':rest
                    })
                }
            }) 
        }
    })
    
})
// 取消订单
router.get('/qxorder',(req,res)=>{
    db.query('delete from gm_shop_order where goods_id ='+req.query.id,(error,result)=>{
        if(error){
            console.log(error);
        }else{
           res.json({
               'ok':1
           })
        }
    })
})
// 全部订单查询
router.post('/showorder',(req,res)=>{
    //  console.log(req.body);
     db.query('select goods_id from gm_shop_order where user_id = ?',req.body.id,(error,result)=>{
        if(error){
            console.log(error);
        }else{
            // console.log(result);   
            let idids = []
            result.forEach(element => {
                idids.push(element.goods_id)
            });
            db.query(`select * from gm_goods where id in (${idids.join(',')})`,(err,rest)=>{
                if(err){
                    console.log(err);
                }else{
                    res.json({
                        'ok':1,
                        'data':rest
                    })
                }
            }) 
        }
    })
})
module.exports = router